// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        startPos: cc.p(0, 100),
        endPos: cc.p(1000, 100),
        speed: 50,
        isTracked: false,
        curAinmationIndex: 0,
        target: {
            default: null,
            type: cc.Animation
        },

    },

    // LIFE-CYCLE CALLBACKS:

    onEnable: function() {

        var animation = this.node.getComponent(cc.Animation);
        animation.on('play', this.onPlay, this);
        animation.on('stop', this.onStop, this);
        animation.on('lastframe', this.onLastFrame, this);
        animation.on('finished', this.onFinished, this);
        animation.on('pause', this.onPause, this);
        animation.on('resume', this.onResume, this);
    },

    onDisable: function() {
        var animation = this.node.getComponent(cc.Animation);

        animation.off('play', this.onPlay, this);
        animation.off('stop', this.onStop, this);
        animation.off('lastframe', this.onLastFrame, this);
        animation.off('finished', this.onFinished, this);
        animation.off('pause', this.onPause, this);
        animation.off('resume', this.onResume, this);
    },
    onLoad() {
        cc.log("FishBase onLoad begin");
        var i = 0;
        var self = this;
        self.initCollider();
        // cc.log(" in onload self.startPos.x is %f , y is %f", self.startPos.x, self.startPos.y);
        self.node.position = self.startPos;
        // cc.log(" in onload self.fishSprite.position.x is %f , y is %f", self.node.position.x, self.node.position.y);
        // this._playAnimCallback = function() {
        //     cc.log("inPlayAimCallback i is " +i);
        //     self.playAnimation(i);
        //     i++;
        //     i = i%5;
        // };

        // this.onEnableCallback();
        // this.schedule(this._playAnimCallback, 2);
        // this.sch
    },

    start() {
        var self = this;

    },
    initStartPos: function(tempPos) {
        var self = this;
        // cc.log(" in start self.startPos.x is %f , y is %f", self.startPos.x, self.startPos.y);
        self.startPos = tempPos;
        self.node.position = self.startPos;
        // cc.log(" after in start self.startPos.x is %f , y is %f", self.startPos.x, self.startPos.y);
    },

    onEnable() {
        var self = this;
        // cc.log(" in onEnable self.startPos.x is %f , y is %f", self.startPos.x, self.startPos.y);
    },

    initCollider: function() {
        cc.director.getCollisionManager().enabled = true;
        cc.director.getCollisionManager().enabledDebugDraw = true;
        cc.director.getCollisionManager().enabledDrawBoundingBox = true;
    },

    onCollisionEnter: function(other) {
        var self = this;
        self.node.color = cc.Color.RED;
        self.playAnimation(2);
        self.fishDropBase = other.getComponent("FishDropBase");

    },

    onCollisionStay: function(other) {
        // console.log('on collision stay');
    },

    onCollisionExit: function() {

    },

    playAnimation: function(index) {
        var animation = this.node.getComponent(cc.Animation);
        this.curAinmationIndex = index;
        switch (index) {
            case 1:
                animation.play("fish-jianyu-jiasu");
                break;
            case 2:
                animation.play("fish-jianyu-yaogou");
                break;
            case 3:
                animation.play("fish-jianyu-zhangzha-a");
                break;
            case 4:
                animation.play("fish-jianyu-zhangzha-b");
                break;
            default:
                animation.play("fish-jianyu-move");
                break;
        }

    },

    initWithTrackData: function(trackData) {
        var self = this;
        this.startPos = trackData.startPos;
        this.endPos = trackData.endPos;

    },

    isOutofRange() {
        var self = this;
        if (self.node.position.x >= self.endPos.x) {
            self.removeSelfFromScene();
            return true;
        }

        return false;
    },

    updatePosition: function(dt) {
        var self = this;
        var delta = self.speed * dt;
        var oldPos = self.node.position;
        var newPos = cc.pAdd(oldPos, cc.p(self.speed * dt, 0));
        self.node.position = newPos;
        // cc.log("self.node.position.x is %f , y is %f", self.node.position.x, self.node.position.y);
    },

    updatePositionBy: function(pos) {
        var self = this;
        self.node.position = pos;
    },

    update: function(dt) {
        var self = this;

        if (self.isOutofRange())
            return;

        if (self.isTracked) {
            // self.updatePositionBy(pos);
            return;
        } else {
            self.updatePosition(dt);
        }


    },

    removeSelfFromScene: function() {
        //to do 

    },


    onPlay: function() {
        cc.log('onPlay ' + this.curAinmationIndex);

    },

    onStop: function() {
        cc.log('onStop' + this.curAinmationIndex);

    },

    onLastFrame: function() {
        cc.log('onLastFrame');

    },

    onFinished: function() {
        cc.log('onFinished');

    },

    onPause: function() {
        cc.log('onPause');

    },

    onResume: function() {
        cc.log('onResume');
    }
});